package com.enqualcomm.kids.mvp.heartrate;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.HeartRateModeQueryParams;
import com.enqualcomm.kids.network.socket.request.HeartRateModeUpdateParams;
import com.enqualcomm.kids.network.socket.request.HeartRateQueryParams;
import com.enqualcomm.kids.network.socket.response.BasicResult;
import com.enqualcomm.kids.network.socket.response.HeartRateModeQueryResult;
import com.enqualcomm.kids.network.socket.response.HeartRateQueryResult;

import common.utils.MyLogger;


/**
 * Created by Sunzhigang on 2016/7/19.
 */
public class HeartRateModel extends NetworkModel implements IHeartRateModel {
    @Override
    public void getHeartRateSettings(String userKey, final String terminalid, final IHeartRateHandler handler) {
        HeartRateModeQueryParams params = new HeartRateModeQueryParams(userKey, terminalid);
        loadDataFromServer(new SocketRequest<HeartRateModeQueryResult>(params, new NetworkListener<HeartRateModeQueryResult>() {
            @Override
            public void onError(VolleyError error) {
                error.printStackTrace();
            }

            @Override
            public void onSuccess(HeartRateModeQueryResult response) {
                if (response.code == 0) {
                    handler.getHeartRateSttingsSuccess(response.result);
                    //保存查询到的数据
                    new TerminalDefault(terminalid).setHeartRateSettings(response.result);
                } else {
                    handler.error(response.code, response.desc);
                }
            }
        }));
    }

    @Override
    public void updateHeartRateSettings(String userKey, String terminalid, HeartRateModeQueryResult.Result info, final IHeartRateHandler handler) {
        HeartRateModeUpdateParams params = new HeartRateModeUpdateParams(userKey, terminalid,
                info.btime,
                info.etime,
                info.mode,
                info.heartrateopen);
        loadDataFromServer(new SocketRequest<BasicResult>(params, new NetworkListener<BasicResult>() {
            @Override
            public void onError(VolleyError error) {
                error.printStackTrace();
            }

            @Override
            public void onSuccess(BasicResult response) {
                if (response.code == 0) {
                    handler.setHeartRateSttingsSuccess();
                } else {
                    handler.error(response.code, response.desc);
                }
            }
        }));
    }

    @Override
    public void queryHeartRate(String userKey, final String terminalid, String strdate, final IHeartRateHandler handler) {
        HeartRateQueryParams params = new HeartRateQueryParams(userKey, terminalid, strdate);
        loadDataFromServer(new SocketRequest<HeartRateQueryResult>(params, new NetworkListener<HeartRateQueryResult>() {
            @Override
            public void onError(VolleyError error) {
                error.printStackTrace();
            }

            @Override
            public void onSuccess(HeartRateQueryResult response) {
                if (response.code == 0) {
                    handler.queryHeartRateSuccess(response.result);
                    //保存查询到的信息
//                    new TerminalDefault(terminalid).setHeartRateList(response);
                } else {
                    handler.error(response.code, response.desc);
                }
            }
        }));
    }
}
